﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using WeGet;

namespace WeAppsCenter;

public struct AppInfo
{
    public string PackageName;
    public string DisplayName;
    public string Version;
    public string IconSource;
    public string Intro;
    public string WhatsNew;
    public string GetLink;
    public bool Enabled;
    public string OpenFile;
    public ChannelInfo[] Channel;

}
public class GetAppInfo
{
    /// <summary>
    /// 根据应用程序的 PackageName 查找应用信息并返回一个 AppInfo 对象
    /// </summary>
    /// <param name="PackageName">应用程序的 PackageName</param>
    /// <returns>返回的 AppInfo 对象</returns>
    public AppInfo GetAppInfoByPackageName(string PackageName)
    {
        AppInfo appInfo = new AppInfo();
        XmlDocument xmlDocument = new XmlDocument();
        WeGet.Apps apps = new WeGet.Apps(Core.Settings.DataDirectory);
        xmlDocument.Load(apps.RepositoryDirectory);
        foreach (XmlNode details in xmlDocument.SelectSingleNode($"/{xmlDocument.DocumentElement.Name}/{PackageName}"))
        {
            if (details.InnerXml != "")
            {

                if (details.Name == "displayname")
                {
                    appInfo.DisplayName = details.InnerText;
                }
                if (details.Name == "version")
                {
                    appInfo.Version = details.InnerText;
                }
                if (details.Name == "intro")
                {
                    appInfo.Intro = details.InnerText;
                }
                if (details.Name == "comment")
                {
                    appInfo.WhatsNew = details.InnerText;
                }
                if (details.Name == "icon")
                {
                    appInfo.IconSource = details.InnerText;
                }
                if (details.Name == "getlink")
                {
                    appInfo.GetLink = details.InnerText;
                }
                if (details.Name == "enabled")
                {
                    if (details.InnerText == "true")
                    {
                        appInfo.Enabled = true;
                    }
                    else if (details.InnerText == "false")
                    {
                        appInfo.Enabled = false;
                    }
                    else
                    {
                        appInfo.Enabled = false;
                        throw new Exception("存储库中有条目拼写不正确，请联系存储库管理员修正后，重新更新存储库。");
                        
                    }
                }
                if (details.Name == "openfile")
                {
                    appInfo.OpenFile = details.InnerText;
                }
                if (details.Name == "channels")
                {
                    var ChannelInfos = new List<ChannelInfo>();
                    
                    foreach (XmlNode channel in xmlDocument.SelectSingleNode($"/{xmlDocument.DocumentElement.Name}/{PackageName}/channels"))
                    {
                        
                        ChannelInfo channelInfo = new ChannelInfo();
                        if (channel.Attributes["name"].Value=="Publish")
                        {
                            channelInfo.Name = Channel.Publish;
                        }
                        else if (channel.Attributes["name"].Value == "Preview")
                        {
                            channelInfo.Name = Channel.Preview;
                        }
                        channelInfo.Icon = channel.Attributes["icon"].Value;
                        channelInfo.Link = channel.Attributes["link"].Value;
                        channelInfo.Identity = channel.Attributes["identity"].Value;
                        ChannelInfos.Add(channelInfo);
                    }
                    ChannelInfo[] channels = ChannelInfos.ToArray();
                    appInfo.Channel = channels;
                }
            }
        }
        appInfo.PackageName = PackageName;
        return appInfo;
    }
    /// <summary>
    /// 根据应用程序的 Manifest 查找应用信息并返回一个 AppInfo 对象
    /// </summary>
    /// <param name="ManifestDirectory">应用程序文件夹下的 Manifest 完整目录</param>
    /// <returns>返回的 AppInfo 对象</returns>
    public AppInfo GetAppInfoByManifest(string ManifestDirectory)
    {
        AppInfo appInfo = new AppInfo();
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.Load(ManifestDirectory);

        foreach (XmlNode details in xmlDocument.SelectSingleNode($"/{xmlDocument.DocumentElement.Name}"))
        {
            if (details.InnerXml != "")
            {

                if (details.Name == "displayname")
                {
                    appInfo.DisplayName = details.InnerText;
                }
                if (details.Name == "version")
                {
                    appInfo.Version = details.InnerText;
                }
                if (details.Name == "intro")
                {
                    appInfo.Intro = details.InnerText;
                }
                if (details.Name == "comment")
                {
                    appInfo.WhatsNew = details.InnerText;
                }
                if (details.Name == "icon")
                {
                    appInfo.IconSource = details.InnerText;
                }
                if (details.Name == "getlink")
                {
                    appInfo.GetLink = details.InnerText;
                }
                if (details.Name == "enabled")
                {
                    if (details.InnerText == "true")
                    {
                        appInfo.Enabled = true;
                    }
                    else if (details.InnerText == "false")
                    {
                        appInfo.Enabled = false;
                    }
                    else
                    {
                        appInfo.Enabled = false;
                        throw new Exception("存储库中有条目拼写不正确，请联系存储库管理员修正后，重新更新存储库。");
                    }
                }
                if (details.Name == "openfile")
                {
                    appInfo.OpenFile = details.InnerText;
                }
                if (details.Name == "channels")
                {
                    var ChannelInfos = new List<ChannelInfo>();

                    foreach (XmlNode channel in xmlDocument.SelectSingleNode($"/{xmlDocument.DocumentElement.Name}/channels"))
                    {

                        ChannelInfo channelInfo = new ChannelInfo();
                        if (channel.Attributes["name"].Value == "Publish")
                        {
                            channelInfo.Name = Channel.Publish;
                        }
                        else if (channel.Attributes["name"].Value == "Preview")
                        {
                            channelInfo.Name = Channel.Preview;
                        }
                        channelInfo.Icon = channel.Attributes["icon"].Value;
                        channelInfo.Link = channel.Attributes["link"].Value;
                        channelInfo.Identity = channel.Attributes["identity"].Value;
                        ChannelInfos.Add(channelInfo);
                    }
                    ChannelInfo[] channels = ChannelInfos.ToArray();
                    appInfo.Channel = channels;
                }
            }
        }

        return appInfo;
    }
}
